package personal.wang.juc;

import java.io.IOException;
import java.util.concurrent.*;

public class CompleatableFutureTest {
    public static void main(String[] args)  {
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
//            try {
//                Thread.sleep(2000L);
//                int i = 1/0 ;
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
            return "a";
        });

        try {
            String s = cf.get();
            System.out.println(s);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        String s2 = cf.join();
        System.out.println(s2);

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
        System.out.println(integerCompletableFuture.join());

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> System.out.println(1), executorService);
        try {
            executorService.awaitTermination(1 , TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//        boolean lili = cf.completeExceptionally(new RuntimeException("lili"));
//        System.out.println(lili);
//        cf.handle((r , t)->{
//            System.out.println(t);
//            return "c";
//        });
//        cf.exceptionally(t -> {
//            System.out.println(t);
//            return "c";
//        });
//        cf.whenComplete((r , t)->{
//            System.out.println(t);
//        });
//        Thread.sleep(6000L);
        try {
            System.in.read();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
